package com.wasion.pwportable.jsi;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.webkit.WebView;

import com.google.gson.Gson;
import com.wasion.pwportable.activity.PwPortableActivity;
import com.wasion.pwportable.domain.vo.SerialPortParam;

/**
 * 
 * @ClassName: ConfigJSI
 * @Description: 系统配置JavaScriptInterface，实现对系统配置的设置<br/>
 *               1. 获取串口通讯参数配置 <br/>
 *               2. 保存串口通讯参数配置 <br/>
 * @author 高意
 * @date 2014-8-13 下午2:13:28
 * 
 */
public class ConfigJSI {

	// 应用上下文
	private Context context;

	// webview，浏览器窗口
	private WebView webView;

	// Google的Json转换工具
	private Gson gson = new Gson();

	private static BluetoothAdapter bluetoothAdapter;

	public ConfigJSI(Context context) {
		this.context = context;
		this.webView = PwPortableActivity.getWebView();
		bluetoothAdapter = PwPortableActivity.getBluetoothAdapter();
	}

	/**
	 * 
	 * @Title: getSerialPortConfigInfo
	 * @Description: 获取串口通讯参数配置信息
	 * @param
	 * @return
	 * @throws
	 */
	public void getSerialPortConfigInfo() {

		SharedPreferences serialPort = context.getSharedPreferences(
				"serialPort", 0);

		String comid = serialPort.getString("comid", "");
		String baud = serialPort.getString("baud", "");
		String check = serialPort.getString("check", "");
		String stop = serialPort.getString("stop", "");
		String data = serialPort.getString("data", "");

		SerialPortParam params = new SerialPortParam();
		params.setComid(comid);
		params.setBaud(baud);
		params.setCheck(check);
		params.setStop(stop);
		params.setData(data);

		String jsonStr = gson.toJson(params);
		webView.loadUrl("javascript:loadSerialPortConfig(" + jsonStr + ")");
	}

	/**
	 * 
	 * @Title: saveSerialPortConfigInfo
	 * @Description: 保存串口通讯参数配置信息
	 * @param
	 * @return
	 * @throws
	 */
	public void saveSerialPortConfigInfo(final String jsonStr) {

		SerialPortParam params = new SerialPortParam();
		params = gson.fromJson(jsonStr, SerialPortParam.class);

		SharedPreferences serialPort = context.getSharedPreferences(
				"serialPort", 0);

		serialPort.edit().putString("comid", params.getComid()).commit();
		serialPort.edit().putString("baud", params.getBaud()).commit();
		serialPort.edit().putString("check", params.getCheck()).commit();
		serialPort.edit().putString("stop", params.getStop()).commit();
		serialPort.edit().putString("data", params.getData()).commit();

		JSIHelper.showToast(context, "保存成功!");
		webView.loadUrl("javascript:saveSuccessCallback()");
	}

	/**
	 * 
	 * @Title: enableBlueTooth
	 * @Description: 启用蓝牙
	 * @param
	 * @return
	 * @throws
	 */
	public void enableBlueTooth() {
		PwPortableActivity.getHandler().post(new Runnable() {
			public void run() {
				if (!bluetoothAdapter.isEnabled()) {
					// 不做提示，强行打开
					bluetoothAdapter.enable();
				}
			}
		});
	}

	/**
	 * 
	 * @Title: disableBlueTooth
	 * @Description: 禁用蓝牙
	 * @param
	 * @return
	 * @throws
	 */
	public void disableBlueTooth() {
		PwPortableActivity.getHandler().post(new Runnable() {
			public void run() {
				if (bluetoothAdapter.isEnabled()) {
					// 不做提示，强行关闭
					bluetoothAdapter.disable();
				}
			}
		});
	}
}
